/*
 * Copyright (c) 2017 5utrip.com. All Rights Reserved.
 */
package com.guoyu.project.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.google.common.collect.Maps;
import com.guoyu.project.model.enums.ArticleType;
import com.guoyu.project.model.enums.ProjectType;
import org.springframework.stereotype.Service;

import com.guoyu.project.model.Article;
import com.guoyu.project.common.constant.Constants;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.dao.ArticleDao;

/**
 * 文章信息,业务类ArticleService
 *
 * @author: xblibo Fri May 26 15:24:04 CST 2017
 * @version 1.0.0
 */
@Service
public class ArticleService {

    @Resource
    private ArticleDao articleDao;

    /**
     * 保存文章信息数据
     *
     * @param article
     */
    public void saveArticle(Article article) {
        articleDao.save(article);
    }

    /**
     * 更新文章信息实体
     *
     * @param article
     */
    public void updateArticle(Article article) {
        articleDao.update(article);
    }

    /**
     * 删除文章信息数据
     *
     * @param articleId
     */
    public void deleteById(int articleId) {
        articleDao.deleteById(articleId);
    }

    /**
     * 根据id获取文章信息数据
     *
     * @param articleId
     * @return
     */
    public Article queryById(int articleId) {
        return articleDao.queryById(articleId);
    }

    /**
     * 根据条件分页查询文章信息数据
     *
     * @param offset
     * @param limit
     * @param params
     * @return
     */
    public Pagination<Article> queryArticles(int offset, int limit, Map<String, Object> params) {
        // 根据条件获取总记录数
        int total = articleDao.queryArticleCount(params);

        if (null == params) {
            params = Maps.newHashMap();
        }
        params.put(Pagination.OFFSET, offset);
        params.put(Pagination.LIMIT, limit);

        // 分页查询数据
        List<Article> articleList = articleDao.queryArticles(params);

        return new Pagination(offset, limit, total, articleList);
    }

    public List<Article> queryArticles(Map<String, Object> params) {
        return articleDao.queryArticles(params);
    }

    public List<Article> queryAllProject() {
        Map<String, Object> params = Maps.newHashMap();
        params.put("majorType", ArticleType.PROJECT);
        params.put("minorType", ProjectType.PROJECT);
        return articleDao.queryArticles(params);
    }

    public List<Article> queryProjectBySchool(int schoolId) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("majorType", ArticleType.PROJECT);
        params.put("minorType", ProjectType.PROJECT);
        params.put("schoolId", schoolId);
        return articleDao.queryArticles(params);
    }

    public List<Article> queryByProjectId(int projectId) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("majorType", ArticleType.PROJECT);
        params.put("minorType", ProjectType.NEWS);
        params.put("projectId", projectId);
        return articleDao.queryArticles(params);
    }
}